import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.HashMap;

public class Generator {
    public static void main(String[] args) {
        // 生成路径
        String projectPath = System.getProperty("user.dir");
        String outputDir = projectPath + "/main/src/main/java";
        String xmlDir = projectPath + "/main/src/main/resources/mapper";
        String otherDir = projectPath + "/main/src/test";

        // 数据库信息
        String host = "localhost";
        String port = "3306";
        String db = "test_db";
        String username = "root";
        String password = "12345678";

        String author = "haojun"; // 作者名
        String basePackage = "tech.horgee"; // 包名
        String baseMapper = "tech.horgee.common.MyBaseMapper";
        String tables = "tab_oper_log"; // 设置需要生成的表名，英文逗号,分割


        FastAutoGenerator.create("jdbc:mysql://" + host + ":" + port + "/" + db + "?serverTimezone=Asia/Shanghai",
                        username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .disableOpenDir()
//                            .enableSwagger() // 开启 swagger 模式
//                            .enableSpringdoc() // 开启 springdoc 模式
                            .outputDir(outputDir); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(basePackage) // 设置父包名
                            .pathInfo(new HashMap<>() {{
                                put(OutputFile.xml, xmlDir);
                            }}); // 设置生成路径
                })
                .templateConfig(builder -> builder
                        .disable() // 禁用默认模板
                        .entity("/templates/entity.java")
                        .mapper("/templates/mapper.java")
                        .xml("/templates/mapper.xml")
                        .service("/templates/service.java")
                        .controller("/templates/controller.java")
                )
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("tab_") // 设置过滤表前缀

                            .controllerBuilder()
                            .enableFileOverride() //覆盖
                            .enableRestStyle()

                            .entityBuilder()
                            .enableFileOverride() //覆盖
                            .enableLombok()
                            .versionColumnName("version")
                            .versionPropertyName("version")
                            .logicDeleteColumnName("deleted")
                            .logicDeletePropertyName("deleted")

                            .serviceBuilder()
                            .enableFileOverride() //覆盖
                            .formatServiceFileName("%sService")

                            .mapperBuilder()
                            .enableFileOverride() //覆盖
                            .mapperAnnotation(org.apache.ibatis.annotations.Mapper.class)
                            .superClass(baseMapper)
                    ;
                })
                .injectionConfig(builder -> builder
                        .customFile(file -> file.fileName("Api.js").templatePath("/templates/api.js.vm").packageName("js").filePath(otherDir).enableFileOverride())
                        .customFile(file -> file.fileName("Api.ts").templatePath("/templates/api.ts.vm").packageName("ts").filePath(otherDir).enableFileOverride())
                        .customFile(file -> file.fileName("Controller.http").templatePath("/templates/controller.http.vm").packageName("http").filePath(otherDir).enableFileOverride())
                        //.customFile(file -> file.fileName("Page.vue").templatePath("/templates/page.vue2.vm").packageName("vue").filePath(otherDir).enableFileOverride())
                        //.customFile(file -> file.fileName("Page.vue").templatePath("/templates/page.vue3.naive.vm").packageName("vue").filePath(otherDir).enableFileOverride())
                        .customFile(file -> file.fileName("Page.vue").templatePath("/templates/page.vue3.antdv.vm").packageName("vue").filePath(otherDir).enableFileOverride())
                )
                .templateEngine(new VelocityTemplateEngine())
                .execute();

    }
}
